package com.ido.news.splashlibrary.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignUtil {

    public static String makeSign(String appId, String appKey, long timeStamp) {
        return md5(sha1(timeStamp + appKey + timeStamp) + appId);
    }


    public static String sha1(String str) {
        return encryption(str, "SHA-1");
    }

    public static String md5(String str) {
        return encryption(str, "MD5");
    }

    public static String encryption(String str, String type) {

        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            digest.update(str.getBytes());

            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();

            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
